package com.birds.portal.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.birds.portal.db.DataBase;
import com.birds.portal.domain.AuthUser;

public class Service {

	private final DataBase db = new DataBase();

	public Service() {
	}

	public ArrayList<AuthUser> getForbiddenAuthUserList() {
		return getAuthUserList();
	}

	public ArrayList<AuthUser> getAuthUserList() {
		String sql = "SELECT * FROM authuser where state ='F'";
		ResultSet rs = null;
		try {
			rs = db.doQuery(sql);
			ArrayList<AuthUser> list = new ArrayList<AuthUser>();
			while (rs.next()) {
				AuthUser authUser = new AuthUser();
				authUser.setIp(rs.getString("ip"));
				authUser.setMac(rs.getString("mac"));
				authUser.setSsid(rs.getString("ssid"));
				authUser.setState(rs.getString("state"));
				list.add(authUser);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	public boolean resume(String mac) {
		String sql = "update authuser set state='0' where mac = '" + mac + "' and state='F'";
		try {
			int state = db.update(sql);
			System.err.println("Update Mac State:" + state);
			if (state == 1) {
				sql = "update authuseronlinetime set onlinetime=0 where mac='" + mac +"'";
				state = db.update(sql);
				System.err.println("MAC Resume Use:" + mac);
			}
			if (state == 1) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public ArrayList<AuthUser> getAuthUserOnlineTimeList() {
		String sql = "SELECT * FROM authuser where state ='F'";
		ResultSet rs = null;
		try {
			rs = db.doQuery(sql);
			while (rs.next()) {

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	public void close() {
		db.close();
	}

}
